﻿namespace EFS.RC.ConversationBrowser.Controls.Browse
{
	public partial class Criteria : EFS.RC.ConversationBrowser.Controls.Control
	{

		#region Delegates

		/// Search
		public delegate void SearchStartedDelegate();
		public SearchStartedDelegate OnSearchStarted;

		/// SearchCompleted
		public delegate void SearchCompletedDelegate(EFS.RC.ConversationBrowser.Tools.Search.ResultList items);
		public SearchCompletedDelegate OnSearchCompleted;

		#endregion


		public Criteria()
		{
			InitializeComponent();

			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//years
				UtilitiesService.Browse_Years_GetAllCompleted += new System.EventHandler<Proxy.Browse_Years_GetAllCompletedEventArgs>(UtilitiesService_Browse_Years_GetAllCompleted);

				//months
				UtilitiesService.Browse_MonthsOfYear_GetAllCompleted += new System.EventHandler<Proxy.Browse_MonthsOfYear_GetAllCompletedEventArgs>(UtilitiesService_Browse_MonthsOfYear_GetAllCompleted);

				//days
				UtilitiesService.Browse_DaysOfMonth_GetAllCompleted += new System.EventHandler<Proxy.Browse_DaysOfMonth_GetAllCompletedEventArgs>(UtilitiesService_Browse_DaysOfMonth_GetAllCompleted);

				//endpoints
				UtilitiesService.Browse_Endpoints_GetAllCompleted += new System.EventHandler<Proxy.Browse_Endpoints_GetAllCompletedEventArgs>(UtilitiesService_Browse_Endpoints_GetAllCompleted);

				//agents
				UtilitiesService.Browse_Agents_GetAllCompleted += new System.EventHandler<Proxy.Browse_Agents_GetAllCompletedEventArgs>(UtilitiesService_Browse_Agents_GetAllCompleted);

				//results
				UtilitiesService.Browse_GetResultsCompleted += new System.EventHandler<Proxy.Browse_GetResultsCompletedEventArgs>(UtilitiesService_Browse_GetResultsCompleted);

				LoadData();
			}
		}

		private void LoadData()
		{
			//years
			UtilitiesService.Browse_Years_GetAllAsync();

			//endpoints
			UtilitiesService.Browse_Endpoints_GetAllAsync();

			//agents
			UtilitiesService.Browse_Agents_GetAllAsync();
		}

		#region WS Callbacks

		void UtilitiesService_Browse_Agents_GetAllCompleted(object sender, Proxy.Browse_Agents_GetAllCompletedEventArgs e)
		{
			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Agent> list =
					EFS.Common.Serialization.Functions.Deserialize<System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Agent>>(e.Result.ToString());

				cbAgents.ItemsSource = list;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data..." + ex.Message);
			}
		}
		 
		void UtilitiesService_Browse_Endpoints_GetAllCompleted(object sender, Proxy.Browse_Endpoints_GetAllCompletedEventArgs e)
		{
			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Endpoint> list =
					EFS.Common.Serialization.Functions.Deserialize<System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Endpoint>>(e.Result.ToString());

				cbEndpoint.ItemsSource = list;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data..." + ex.Message);
			}
		}

		void UtilitiesService_Browse_DaysOfMonth_GetAllCompleted(object sender, Proxy.Browse_DaysOfMonth_GetAllCompletedEventArgs e)
		{
			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Day> list =
					EFS.Common.Serialization.Functions.Deserialize<System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Day>>(e.Result.ToString());

				cbDays.IsEnabled = true;
				cbDays.ItemsSource = list;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data..." + ex.Message);
			}
		}

		void UtilitiesService_Browse_MonthsOfYear_GetAllCompleted(object sender, Proxy.Browse_MonthsOfYear_GetAllCompletedEventArgs e)
		{
			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Month> list =
					EFS.Common.Serialization.Functions.Deserialize<System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Month>>(e.Result.ToString());

				cbMonth.IsEnabled = true;
				cbMonth.ItemsSource = list;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data..." + ex.Message);
			}
		}

		void UtilitiesService_Browse_Years_GetAllCompleted(object sender, Proxy.Browse_Years_GetAllCompletedEventArgs e)
		{
			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Year> list =
					EFS.Common.Serialization.Functions.Deserialize<System.Collections.Generic.List<EFS.RC.ConversationBrowser.Tools.Browse.Year>>(e.Result.ToString());

				cbYear.ItemsSource = list;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data..." + ex.Message);
			}
		}

		void UtilitiesService_Browse_GetResultsCompleted(object sender, Proxy.Browse_GetResultsCompletedEventArgs e)
		{
			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.ConversationBrowser.Tools.Search.ResultList resultList =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.ConversationBrowser.Tools.Search.ResultList>(e.Result.ToString());

				// call registered delegates
				if (null != this.OnSearchCompleted)
				{
					this.OnSearchCompleted(resultList);
				}
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve events.", ex);
			}
		}

		#endregion

		#region UI Events

		private void cbYear_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
			cbMonth.IsEnabled = false;
			cbDays.IsEnabled = false;

			EFS.RC.ConversationBrowser.Tools.Browse.Year year = (EFS.RC.ConversationBrowser.Tools.Browse.Year)cbYear.SelectedItem;

			UtilitiesService.Browse_MonthsOfYear_GetAllAsync(year.Yr);
		}

		private void cbMonth_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
			cbDays.IsEnabled = false;

			EFS.RC.ConversationBrowser.Tools.Browse.Year year = (EFS.RC.ConversationBrowser.Tools.Browse.Year) cbYear.SelectedItem;
			EFS.RC.ConversationBrowser.Tools.Browse.Month month = (EFS.RC.ConversationBrowser.Tools.Browse.Month) cbMonth.SelectedItem;

			UtilitiesService.Browse_DaysOfMonth_GetAllAsync(year.Yr, month.Code);
		}

		private void buttonBrowse_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			System.Int32? year = ((cbYear.SelectedItem != null) ? (System.Int32?)((EFS.RC.ConversationBrowser.Tools.Browse.Year)(cbYear.SelectedItem)).Yr : null);
			System.Int32? month = ((cbMonth.SelectedItem != null) ? (System.Int32?)((EFS.RC.ConversationBrowser.Tools.Browse.Month)(cbMonth.SelectedItem)).Code : null);
			System.Int32? day = ((cbDays.SelectedItem != null) ? (System.Int32?)((EFS.RC.ConversationBrowser.Tools.Browse.Day)(cbDays.SelectedItem)).Code : null);
			System.Int32? endpoint_id = ((cbEndpoint.SelectedItem != null) ? (System.Int32?)((EFS.RC.ConversationBrowser.Tools.Browse.Endpoint)(cbEndpoint.SelectedItem)).ID : null);
			System.String agent_guid = ((cbAgents.SelectedItem != null) ? ((EFS.RC.ConversationBrowser.Tools.Browse.Agent)(cbAgents.SelectedItem)).Guid.ToString() : "");

			UtilitiesService.Browse_GetResultsAsync(year, month, day, endpoint_id, agent_guid);

			// call registered delegates
			if (null != OnSearchStarted)
			{
				this.OnSearchStarted();
			}
		}

		#endregion
	}
}
